/*************************************************************************
 *                                                                       *
 * This file is part of the DogosFrame distribution.  For full copyright *
 * information, see COPYRIGHT and COPYING.LESSER.                        *
 *                                                                       *
 * Author:           sqbang                                              *
 * Email:            sqbang@ustc.edu                                     *
 *                                                                       *
 *************************************************************************/

//declaration header
#include "COMMON/tbox/DogosManager.h"

//COMMON headers
#include "COMMON/tbox/MathUtilities.h"
#include "COMMON/tbox/IEEE.h"

namespace DOGOS
{

/*
 *************************************************************************
 *
 * Set static members to set maximum number of patch data entries,
 * statistics, and timers supported by the code.
 * These numbers are used to set the sizes of certain array containers
 * used in the SAMRAI library.  They are set here so that they may be
 * resized if necessary from a single access point (i.e., via the
 * DogosManager) if the default sizes (set here) are insufficient.
 *
 * These values can be changed by calling the static functions:
 * DogosManager::set_max_num_patch_data_entries(),
 *
 * To avoid potentially erroneous or unexpected behavior, these
 * value should be set early on during program execution before they
 * are accessed in the library. Once accessed within the library,
 * they cannot be reset during program execution.
 *
 *************************************************************************
 */

bool DogosManager::s_initialized                     = false;
bool DogosManager::s_started                         = false;
int  DogosManager::s_max_patch_data_entries          = 256;
bool DogosManager::s_max_patch_data_entries_accessed = false;

/*
 *************************************************************************
 *
 * Initialize the SAMRAI package.  This routine performs the following
 * tasks:
 *
 * (1) Initialize the SAMRAI MPI package
 * (2) Initialize the parallel I/O routines
 * (3) Set up IEEE assertion handlers
 * (4) Set new handler so that an error message is printed if new fails.
 *
 *************************************************************************
 */

static void
badnew()
{
    TBOX_ERROR ("operator `new' failed -- program abort!" << std::endl);
}



void
DogosManager::initialize(
    bool initialize_IEEE_assertion_handlers)
{
    TBOX_ASSERT (!s_initialized);

    PIO::initialize();

    if (initialize_IEEE_assertion_handlers)
    {
        IEEE::setup_floating_point_exception_handlers();
    }

    std::set_new_handler(badnew);

    StartupShutdownManager::initialize();

    s_initialized = true;
}



void
DogosManager::set_max_num_patch_data_entries(
    int maxnum)
{
    if (s_max_patch_data_entries_accessed)
    {
        TBOX_ERROR (
            "DogosManager::set_max_num_patch_data_entries() error..."
            << "\nThe max patch data entries value has already been accessed and cannot"
            << "\nbe reset after that point by calling this method -- program abort!"
            << std::endl);
    }
    else
    {
        s_max_patch_data_entries = MathUtilities<int>::max(
                                       maxnum,
                                       s_max_patch_data_entries);
    }
}

}
